package net.wanho.shop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import net.wanho.shop.dto.LoginUserDTO;
import net.wanho.shop.service.LoginService;
import net.wanho.shop.util.CurrentUtil;
import net.wanho.shop.vo.AjaxResult;
import net.wanho.shop.vo.CaptchaVo;
import net.wanho.shop.vo.LoginUserVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 这个类是：
 *
 * @author: MENG HU WANG
 * @date: 2024/12/24 21:52
 * @version: 1.0
 */
@Api(tags = "01.登录接口")
@RestController
@RequiredArgsConstructor
public class LoginController {

    private final LoginService loginService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public AjaxResult login(@ApiParam("用户登录信息") @RequestBody LoginUserDTO loginUserDTO) {
        LoginUserVo loginUserVo = loginService.login(loginUserDTO);
        return AjaxResult.success(loginUserVo);
    }

    @ApiOperation("获取验证")
    @GetMapping("/captchaImage")
    public AjaxResult captchaImage() {
        CaptchaVo captchaVo = loginService.captcha();
        return AjaxResult.success(captchaVo);
    }

    @ApiOperation("带验证码的登录")
    @PostMapping("/captchaLogin")
    public AjaxResult captchaLogin(@RequestBody LoginUserDTO loginUserDTO) {
        LoginUserVo loginUserVo = loginService.captchaLogin(loginUserDTO);
        return AjaxResult.success(loginUserVo);
    }

    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public AjaxResult logout() {
        CurrentUtil.removeId();
        return AjaxResult.success();
    }
}
